package cn.dnui_xqh414.dnuifood_16110100414.fragment_adapter_and_vhoder;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.chip.Chip;
import com.google.android.material.snackbar.Snackbar;
import com.squareup.picasso.Picasso;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;
import cn.dnui_xqh414.dnuifood_16110100414.FoodDetailActivity;
import cn.dnui_xqh414.dnuifood_16110100414.R;
import cn.dnui_xqh414.dnuifood_16110100414.ShopFoodListActivity;
import cn.dnui_xqh414.dnuifood_16110100414.bean.ResultBean;
import cn.dnui_xqh414.dnuifood_16110100414.bean.ShopdetailBean;
import cn.dnui_xqh414.dnuifood_16110100414.bean.getAllCommentsBean;
import cn.dnui_xqh414.dnuifood_16110100414.listener.OnRetrofitListener;
import cn.dnui_xqh414.dnuifood_16110100414.model.FoodlistModel;
import cn.dnui_xqh414.dnuifood_16110100414.model.UserModel;

public class Rv_Adapter_orderList
        extends RecyclerView.
        Adapter<RecyclerView.ViewHolder>
        implements RecycleItemTouchHelper.ItemTouchHelperCallback{

    private Context context;
    private List<getAllCommentsBean> list;
    private LayoutInflater inflater;

    private String upload_comment = "";

    private FoodlistModel model;

    public Rv_Adapter_orderList(Context context){

        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setList(List<getAllCommentsBean> list){
        this.list = list;

        notifyDataSetChanged();   //十分重要
    }

    @Override
    public void onItemDelete(int position) {
        list.remove(position);

        notifyItemRemoved(position);
    }

    class orderViewHolder extends
            RecyclerView.ViewHolder {

        public View itemView;

        public LinearLayout linearLayout;
        public TextView shopname,state,price,totalPrice,num,foodname;
        public Chip comment;
        public ImageView imageView;

        public orderViewHolder(@NonNull View itemView) {
            super(itemView);
            linearLayout = itemView.findViewById(R.id.ll_toShopDetail);
            shopname = itemView.findViewById(R.id.tv_order_shopname);
            state = itemView.findViewById(R.id.tv_order_state);
            price = itemView.findViewById(R.id.tv_order_price);
            totalPrice = itemView.findViewById(R.id.tv_order_totalPrice);
            num = itemView.findViewById(R.id.tv_order_num);
            comment = itemView.findViewById(R.id.chip_comment);
            foodname = itemView.findViewById(R.id.tv_order_foodname);

            imageView = itemView.findViewById(R.id.img_order_foodimg);

            this.itemView = itemView;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder
    onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.rv_item_order_list,
                        parent ,false);
        return new orderViewHolder(v);    //形同于 shopVIewHolder hoder = new ~;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final getAllCommentsBean commentsBean = list.get(position);
        final orderViewHolder viewHolder = (orderViewHolder) holder;

        if (commentsBean == null) return;

        viewHolder.totalPrice.setText("￥"+String.valueOf(commentsBean.getNum()*commentsBean.getPrice()));
        viewHolder.price.setText("￥"+String.valueOf(commentsBean.getPrice()));
        if (commentsBean.getState() == 0)
        viewHolder.state.setText("未付款");
        else
            viewHolder.state.setText("已付款");
        viewHolder.shopname.setText(commentsBean.getShopname());
        viewHolder.num.setText("x"+String.valueOf(commentsBean.getNum()));
        viewHolder.foodname.setText(commentsBean.getFoodname());

        // TODO: 12/17/2018 img
        viewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v,"跳转到shop界面尚未完成",Snackbar.LENGTH_SHORT).show();
            }
        });

        viewHolder.comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                get_comment(position);
            }
        });

        model = new FoodlistModel();
        model.getFoodbyId(list.get(position).getFood_id(), new OnRetrofitListener() {
            @Override
            public void onSuccess(Object object, int flag) {
                ShopdetailBean bean = (ShopdetailBean) object;
                String url =
                        "http://172.24.10.175:8080/foodService/"+bean.getPic();
                Picasso.get()
                        .load(url)
                        .placeholder(R.drawable.ic_loading)
                        .error(R.drawable.ic_load_fail_sfont)
                        .fit()
                        .centerCrop()
                        .into(viewHolder.imageView);
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    @Override
    public int getItemCount() {
        if (list == null)
        return 0;
        return list.size();
    }

    private void get_comment(final int position) {
        LayoutInflater factory = LayoutInflater.from(context);
        @SuppressLint("InflateParams") View commentView = factory.inflate(R.layout.float_edit, null);
        // removeView();
        //内部局部类，只能访问方法的final类型的变量
        final EditText comment = (EditText) commentView
                .findViewById(R.id.et_comment);
        // 不是用这个方法获取EditText的内容的
        // mname_edit.addTextChangedListener(mTextWatcher);

        // create a dialog
        new AlertDialog.Builder(context)
                .setIcon(R.drawable.ic_cherry_99)
                .setTitle("")
                .setView(commentView)
                .setNegativeButton("CANCEL",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                // TODO Auto-generated method stub
                                dialog.cancel();
                            }
                        })
                .setPositiveButton("CONFIRM",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                // dialog.
                                // TODO Auto-generated method stub
                                if (!comment.getText().toString().equals("")) {
                                    upload_comment = comment.getText().toString();
                                    UserModel model = new UserModel();
                                    model.insertComment(list.get(position).getItem_id(), upload_comment, new OnRetrofitListener() {
                                        @Override
                                        public void onSuccess(Object object, int flag) {
                                            ResultBean bean = (ResultBean) object;
                                            if (bean.getSuccess().equals("1")){
                                                Toast.makeText(context, "评论成功", Toast.LENGTH_SHORT).show();
                                            }else {
                                                Toast.makeText(context, "未知错误", Toast.LENGTH_SHORT).show();
                                            }
                                        }

                                        @Override
                                        public void onFailure(String msg) {

                                        }
                                    });
                                }
                            }
                        }).show();
    }
}
